package cn.fzkj.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.fzkj.common.utils.PageUtils;
import cn.fzkj.common.utils.Query;

import cn.fzkj.gulimall.product.dao.CategoryDao;
import cn.fzkj.gulimall.product.entity.CategoryEntity;
import cn.fzkj.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    // 查询分类的tree列表
    @Override
    public List<CategoryEntity> listByTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        return categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu -> {
                    menu.setChildrens(getChildrens(menu, categoryEntities));
                    return menu;
                })
                /** 变更记录
                 *                 .sorted((first, second) -> {
                 *                     return (first.getSort()==null?0:first.getSort()) - (second.getSort()==null?0:second.getSort());
                 *                 })
                 */
                .sorted(Comparator.comparingInt(categoryEntity -> (categoryEntity.getSort() == null ? 0 : categoryEntity.getSort())))
                .collect(Collectors.toList());
    }

    @Override
    public boolean removeNotUsedMenuByIds(Long[] catIds) {
        // TODO 检查菜单是否被引用
        baseMapper.deleteBatchIds(Arrays.asList(catIds));
        return false;
    }

    @Override
    public List<CategoryEntity> getCategories() {

        return this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("cat_level", 1));
    }

    @Override
    public Long[] findCategoryPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        findParentPath(catelogId, path);
        return path.toArray(new Long[path.size()]);
    }

    private void findParentPath(Long catelogId, List<Long> path){
        CategoryEntity entity = this.getById(catelogId);
        if (entity.getParentCid() != null && entity.getParentCid() != 0){
            findParentPath(entity.getParentCid(), path);
        }
        path.add(entity.getCatId());
//        return path;
    }

    /**
     * 递归查找所有的子菜单
     * @param root
     * @param all
     * @return
     */
    private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all){
        return all.stream().filter(entity -> entity.getParentCid() == root.getCatId())
                .map(menu -> {
                    menu.setChildrens(getChildrens(menu, all));
                    return menu;
                })
                /** 变更记录
                 * //                .sorted((first, second) -> {
                 * //                    return (first.getSort() == null ? 0 : first.getSort()) - (second.getSort() == null ? 0 : second.getSort());
                 * //                })
                 */
                .sorted(Comparator.comparingInt(categoryEntity -> (categoryEntity.getSort() == null ? 0 : categoryEntity.getSort())))
                .collect(Collectors.toList());
    }
}